//
//  SelectIncomeCategory.m
//  Wallets Manager
//
//  Created by Tung Nguyen on 10/8/12.
//  Copyright (c) 2012 BKHN. All rights reserved.
//

#import "SelectIncomeCategory.h"

@interface SelectIncomeCategory ()

@end

@implementation SelectIncomeCategory
@synthesize categoryTableView;
@synthesize incomeCategoryArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //-----------------------Init Image Array---------------------//
    
    incomeCategoryArray = [[NSMutableArray alloc]initWithArray:[DataBaseAccess getAllIncomeType]];
    
    // Do any additional setup after loading the view from its nib.
    categoryTableView.tableFooterView = nil;
    categoryTableView.tableHeaderView = nil;
    //categoryTableView.frame = CGRectMake(0, 40, 320, 371);
}

- (void)viewDidUnload
{
    [self setCategoryTableView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - IBAction Delegate
- (IBAction)backButtonPressed:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Table View Delegate

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [incomeCategoryArray count];
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString* cellIndentifier = @"CellIndentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    IncomeType *tempIncomeType = [incomeCategoryArray objectAtIndex:indexPath.row];
    
    
    UIImageView *categoryImageView;
    UILabel     *categoryLabel;
    UIImageView *backgroundImageView;
    
    if( cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
        //cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        backgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
        backgroundImageView.tag = 300;
        [cell addSubview:backgroundImageView];
        
        categoryImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 40, 40)];
        categoryImageView.backgroundColor = [UIColor clearColor];
        categoryImageView.tag = 100;
        [cell addSubview:categoryImageView];
        
        categoryLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 10, 150, 30)];
        categoryLabel.backgroundColor = [UIColor clearColor];
        categoryLabel.textColor = [UIColor colorWithRed:99.0/255.0 green:182.0/255.0 blue:67.0/255.0 alpha:1];
        categoryLabel.tag = 200;
        [cell addSubview:categoryLabel];
    }
    
    backgroundImageView = (UIImageView *) [cell viewWithTag:300];
    [backgroundImageView setImage:[UIImage imageNamed:@"cell_bg_blank.png"]];
    
    categoryImageView = (UIImageView*)[cell viewWithTag:100];
    [categoryImageView setImage:[UIImage imageNamed:tempIncomeType.it_image]];
     
    //categoryImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]]];
    
    categoryLabel = (UILabel*) [cell viewWithTag:200];
    categoryLabel.text = tempIncomeType.it_name;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    currentIncomeType = [incomeCategoryArray objectAtIndex:indexPath.row];
    
    NSDictionary *tempDict = [[NSDictionary alloc]initWithObjectsAndKeys: currentIncomeType,@"key", nil];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"getCategoryOnSuccess" object:tempDict];
    [self.navigationController popViewControllerAnimated:YES];
}
@end
